#include "drivers.h"

/**
 * @brief  Initialize Usart1 with certain baudrate
 *
 * @param   baudrate for usart1
 * @return  none
 *
 */

void Usart1_Init (uint32_t baudrate)
{    
	USART_InitTypeDef Usart1_InitStructure;
	GPIO_InitTypeDef RTX_InitStructure;


	RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);


	RTX_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	RTX_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

	RTX_InitStructure.GPIO_Pin = GPIO_Pin_9;  
	GPIO_Init (GPIOA, &RTX_InitStructure);

	RTX_InitStructure.GPIO_Pin = GPIO_Pin_10;
	RTX_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init (GPIOA, &RTX_InitStructure);

	Usart1_InitStructure.USART_BaudRate = baudrate;
	Usart1_InitStructure.USART_WordLength = USART_WordLength_8b;
	Usart1_InitStructure.USART_StopBits = USART_StopBits_1;
	Usart1_InitStructure.USART_Parity = USART_Parity_No;
	Usart1_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	Usart1_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init (USART1, &Usart1_InitStructure);

	USART_Cmd (USART1, ENABLE);
	USART_ClearFlag (USART1, USART_FLAG_TC); /* Clear This Bit To Make Sure That 1st Byte Can Be Sent Out Correctly */

}

/**
 * @brief  Initialize Usart2 with certain baudrate
 *
 * @param   baudrate for Usart2
 * @return  none
 *
 */

void Usart2_Init (uint32_t baudrate)
{    
	USART_InitTypeDef Usart2_InitStructure;
	GPIO_InitTypeDef RTX_InitStructure;


	RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB1Periph_USART2, ENABLE);


	RTX_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	RTX_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

	RTX_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_Init (GPIOA, &RTX_InitStructure);

	RTX_InitStructure.GPIO_Pin = GPIO_Pin_3;
	RTX_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init (GPIOA, &RTX_InitStructure);

	Usart2_InitStructure.USART_BaudRate = baudrate;
	Usart2_InitStructure.USART_WordLength = USART_WordLength_8b;
	Usart2_InitStructure.USART_StopBits = USART_StopBits_1;
	Usart2_InitStructure.USART_Parity = USART_Parity_No;
	Usart2_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	Usart2_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init (USART2, &Usart2_InitStructure);

	USART_Cmd (USART2, ENABLE);
	USART_ClearFlag (USART2, USART_FLAG_TC); /* Clear This Bit To Make Sure That 1st Byte Can Be Sent Out Correctly */

}

/**
 * @brief  Put the char received in Usart1 to certain string
 *
 * @param   pointer of the string you want to keep the received 
 *  		char
 * @return  none
 *
 */

void Usart1_Scanf(uint8_t *ScanfBuf)
{  
	u8 temp;
	do 
	{
		while (!USART_GetFlagStatus(USART1,USART_FLAG_RXNE));
		temp = USART1->DR;  
		if (temp!='\b')

			*ScanfBuf = temp;
		else
			ScanfBuf-=2; 

		USART_SendData(USART1,temp);
		while (!USART_GetFlagStatus(USART1,USART_FLAG_TC));

	}
	while (*ScanfBuf++!=0xD);
	ScanfBuf--;
	*ScanfBuf=0;
	USART_SendData(USART1,'\r');
	while (!USART_GetFlagStatus(USART1,USART_FLAG_TC));
	USART_SendData(USART1,'\n');
	while (!USART_GetFlagStatus(USART1,USART_FLAG_TC));
}

